package com.huanzhidadi.feignservice.controller;

import com.alibaba.fastjson2.JSON;
import com.huanzhidadi.feignservice.config.UserContextHolder;
import com.huanzhidadi.feignservice.model.BaseResultVO;
import com.huanzhidadi.feignservice.model.ItemVO;
import com.huanzhidadi.feignservice.model.LoginUser;
import com.huanzhidadi.feignservice.util.RedisUtil;
import com.huanzhidadi.feignservice.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.security.sasl.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("feign")
public class FeignController {

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value = "requestBody", method = RequestMethod.POST)
    public BaseResultVO requestBody(@RequestBody ItemVO itemVO) {
        System.out.println("requestBody");
        for (String itemId : itemVO.getItemIds()) {
            System.out.println(itemId);
        }
        return BaseResultVO.success(Arrays.asList(itemVO.getItemIds()));
    }

    @RequestMapping(value = "requestDelay", method = RequestMethod.POST)
    public BaseResultVO requestDelay(@RequestBody ItemVO itemVO) {
        System.out.println("requestDelay");
        try {
            Thread.sleep(3000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (String itemId : itemVO.getItemIds()) {
            System.out.println(itemId);
        }
        return BaseResultVO.success(Arrays.asList(itemVO.getItemIds()));
    }

    @RequestMapping(value = "transUserToken", method = RequestMethod.POST)
    public BaseResultVO transUserToken(@RequestBody ItemVO itemVO, @RequestParam String token) throws Exception {
        log.info("token: " + token);
        String userId = tokenUtil.getIdFromToken(token);

        // 从redis中获取用户信息
        LoginUser loginUser = redisUtil.getObject("security:" + userId, LoginUser.class);
        if (!Objects.isNull(loginUser)) {
            log.info(JSON.toJSONString(loginUser));
        }

        for (String itemId : itemVO.getItemIds()) {
            log.info(itemId);
        }
        return BaseResultVO.success(Arrays.asList(itemVO.getItemIds()));
    }

    @RequestMapping(value = "transUserWithoutToken", method = RequestMethod.POST)
    public BaseResultVO transUserWithoutToken(@RequestBody ItemVO itemVO) {

        // 从redis中获取用户信息
        LoginUser loginUser = UserContextHolder.currentLoginUser();
        if (!Objects.isNull(loginUser)) {
            log.info(JSON.toJSONString(loginUser));
        }

        for (String itemId : itemVO.getItemIds()) {
            log.info(itemId);
        }
        return BaseResultVO.success(Arrays.asList(itemVO.getItemIds()));
    }


    @RequestMapping(value = "transUserWithGetReq", method = RequestMethod.GET)
    public BaseResultVO transUserWithGetReq() {
        // 从redis中获取用户信息
        LoginUser loginUser = UserContextHolder.currentLoginUser();
        if (!Objects.isNull(loginUser)) {
            log.info(JSON.toJSONString(loginUser));
        }
        return BaseResultVO.success();
    }
}
